<?php
namespace app\common\model;
use think\Model;
class Caiji extends Model{	
    public function vod($param){
        if($param['type'] == '1'){
            return $this->vod_xml($param);
        }
        elseif($param['type'] == '2'){
            return $this->vod_json($param);
        }
        else{
            $data = $this->vod_json($param);
            if($data['code'] == 1){
                return $data;
            }
            else{
                return $this->vod_xml($param);
            }
        }			
	}
    public function vod_json($param){
        $url_param = [];
		$url_param['v'] = 9;
		$url_param['action'] = !empty($param['ac']) ? $param['ac'] : '';
		$url_param['ac'] = !empty($param['ac']) ? $param['ac'] : '';
		$url_param['cid'] = $param['cid'];
        $url_param['h'] = !empty($param['h']) ? $param['h'] : '';
        $url_param['vodids'] =!empty($param['ids']) ? $param['ids'] : '';
		$url_param['ids'] = !empty($param['ids']) ? $param['ids'] : '';
        $url_param['wd'] = $param['wd'];		
		$url_param['p'] = $param['p'] ? $param['p'] : '';	
        $url = base64_decode($param['apiurl']);
        if(strpos($url,'?')===false){
            $url .='?';
        }
        else{
            $url .='&';
        }
        $url .= http_build_query($url_param). base64_decode($param['param']);
        $html = zanpian_curl_get($url);	
        if(empty($html)){
			return ['code'=>-1, 'msg'=>'连接API资源库失败，通常为服务器网络不稳定或禁用了采集。'];
		}		
        $json = json_decode($html,true);
		if(is_null($json)){
			return ['code'=>-2, 'msg'=>'JSON格式不正确，不支持采集。'];
		}		
		// 获取到的远程分页数据
		$array_page = array();
		$array_page['pageindex'] = $json['page']['pageindex'];
		$array_page['pagecount'] = $json['page']['pagecount'];
		$array_page['pagesize'] = $json['page']['pagesize'];
		$array_page['recordcount'] = $json['page']['recordcount'];
		if(!in_array($param['ac'], array('ids','day','all','week','post')) ){
		    $param['p'] = 'zanpianpage';
		}
        $array_page['pagelist'] = adminpage($array_page['pageindex'],$array_page['pagecount'],3,url('api',$param),'');			
		// 获取到的远程栏目数据	
		$array_list = array();
		foreach($json['list'] as $key=>$value){
			$array_list[$key]['list_id'] = $value['list_id'];
			$array_list[$key]['list_name'] = $value['list_name'];
			$array_list[$key]['bind_id'] = $value['list_id'];
		}	
		if($json['data']){
			foreach($json['data'] as $key=>$value){
				$json['data'][$key]['vod_cid'] = intval(collect_bind_id($param['apiid'],$value['vod_cid']));
				$json['data'][$key]['vod_name'] =htmlspecialchars_decode($value['vod_name']);
				$json['data'][$key]['vod_inputer'] = $param['apiid'].'_'.$value['vod_id'];
				if(empty($json['data'][$key]['vod_reurl'])){
					$json['data'][$key]['vod_reurl'] = $param['apiid'].'_'.$value['vod_id'];
				}
			}
		}
		$array['page'] = $array_page; //远程分页数据
		$array['list'] = $array_list; //远程栏目数据
		$array['data'] = $json['data']; //远程内容数据
		return ['code'=>200, 'type'=>'json','data'=>$array];
		
	}
    public function vod_xml($param){
        $url_param = [];
		if($param['ac']=='show'){
			$url_param['ac'] = 'list';
		}
        $url_param['t'] = $param['cid'];
		$url_param['cid'] = $param['cid'];
        $url_param['pg'] = is_numeric($param['p']) ? $param['p'] : '';
        $url_param['h'] = $param['h'];
        $url_param['ids'] = $param['ids'];
        $url_param['wd'] = $param['wd'];
        if(empty($param['h']) && !empty($param['rday'])){
            $url_param['h'] = $param['rday'];
        }
        if($param['ac']!='show'){
            $url_param['ac'] = 'videolist';
        }
		$url = base64_decode($param['apiurl']);
        if(strpos($url,'?')===false){
            $url .='?';
        }
        else{
            $url .='&';
        }
        $url .= http_build_query($url_param). base64_decode($param['param']);
		$url=str_replace('?ac=list&ac=','?ac=',$url);
		
        $html = zanpian_curl_get($url);
        if(empty($html)){
            return ['code'=>-1, 'msg'=>'连接API资源库失败，通常为服务器网络不稳定或禁用了采集。'];
        }
        $xml = @simplexml_load_string($html);		
        if(empty($xml)){
            $labelRule = '<pic>'."(.*?)".'</pic>';
            $labelRule = '/'.str_replace('/','\/',$labelRule).'/is';
            preg_match_all($labelRule,$html,$tmparr);
            $ec=false;
            foreach($tmparr[1] as $tt){
                if(strpos($tt,'[CDATA')===false){
                    $ec=true;
                    $ne = '<pic>'.'<![CDATA['.$tt .']]>'.'</pic>';
                    $html = str_replace('<pic>'.$tt.'</pic>',$ne,$html);
                }
            }
            if($ec) {
                $xml = @simplexml_load_string($html);
            }
            if(empty($xml)) {
                return ['code' => -3, 'msg' => 'XML格式不正确，不支持采集'];
            }
        }
		// 获取到的远程分页数据
		$array_page = array();
		$array_page['pageindex'] = (string)$xml->list->attributes()->page;
		$array_page['pagecount'] = (string)$xml->list->attributes()->pagecount;
		$array_page['pagesize'] = (string)$xml->list->attributes()->pagesize;
		$array_page['recordcount'] = (string)$xml->list->attributes()->recordcount;
		if(!in_array($param['ac'], array('ids','day','all','week','post')) ){
		    $param['p'] = 'zanpianpage';
		}
        $array_page['pagelist'] = adminpage($array_page['pageindex'],$array_page['pagecount'],3,url('api',$param),'');		
		// 获取到的远程栏目数据	
		$array_list = array();
		if($xml->class->ty){
			$key=0;
            foreach($xml->class->ty as $ty){
                $array_list[$key]['list_id'] = (string)$ty->attributes()->id;
                $array_list[$key]['list_name'] = (string)$ty;
				$array_list[$key]['bind_id'] = (string)$ty->attributes()->id;
                $key++;
            }
		}
		if($xml->list->video){
			foreach($xml->list->video as $video){
				$listvod[$key]['vod_addtime'] = (string)$video->last;
				$listvod[$key]['vod_id'] = (string)$video->id;
				$listvod[$key]['vod_tid'] = (string)$video->tid;
				$listvod[$key]['vod_cid'] = intval(collect_bind_id($param['apiid'],$listvod[$key]['vod_tid']));
				$listvod[$key]['vod_name'] = (string)$video->name;
				$listvod[$key]['list_name'] =  (string)$video->type;
				//以下参数只在内容页才有
				$listvod[$key]['vod_pic'] = (string)$video->pic;
				$listvod[$key]['vod_keywords'] = (string)$video->keywords;
				$listvod[$key]['vod_total'] = 0;				
				$listvod[$key]['vod_filmtime'] = (string)$video->filmtime;
				$listvod[$key]['vod_tv'] = (string)$video->tv;
				$listvod[$key]['vod_diantai'] = (string)$video->diantai; 
				$listvod[$key]['vod_aliases']=(string)$video->aliases;
				$listvod[$key]['vod_language'] = (string)$video->lang;
				$listvod[$key]['vod_area'] = (string)$video->area;
				$listvod[$key]['vod_year'] = (string)$video->year;
				$listvod[$key]['vod_continu'] = (string)$video->state;
				$listvod[$key]['vod_actor'] = (string)$video->actor;
				$listvod[$key]['vod_director'] = (string)$video->director;
				$listvod[$key]['vod_content'] = (string)$video->des;
				$listvod[$key]['vod_inputer'] = (string)$param['apiid'].'_'.$listvod[$key]['vod_id'];
				$listvod[$key]['vod_reurl'] = (string)$video->reurl;
                $listvod[$key]['vod_title'] =(string)$video->note;					
				if(!$listvod[$key]['vod_reurl']){
					$listvod[$key]['vod_reurl'] = $listvod[$key]['vod_inputer'];
				}
			    //格式化地址与播放器
			    $vod_play = array();
			    $vod_url = array();
				if($video->dl->dd){
				    for($i=0; $i<count($video->dl->dd); $i++){
					$vod_play[$i] = (string)$video->dl->dd[$i]['flag'];
					$vod_url[$i] = $this->xml_url_replace((string)$video->dl->dd[$i]);
				    }
				}else{	
				    $vod_play[]=(string)$video->dt;
				}
				$listvod[$key]['vod_play'] = implode('$$$', $vod_play);
			    $listvod[$key]['vod_url'] = implode('$$$', $vod_url);
				$key++;

			}
		}		
		$array['page'] = $array_page; //远程分页数据
		$array['list'] = $array_list; //远程栏目数据
		$array['data'] = $listvod; //远程内容数据
		return ['code'=>200, 'type'=>'xml','data'=>$array];
    }
    public function order($param){
        $url_param = [];
		$url_param['v'] = 9;
		$url_param['action'] = $param['ac'];
		$url_param['ac'] = $param['ac'];
		$url_param['cid'] = $param['cid'];
        $url_param['h'] = $param['h'];
        $url_param['vodids'] = $param['ids'];
		$url_param['ids'] = $param['ids'];
        $url_param['wd'] = $param['wd'];		
		$url_param['p'] = $param['p'] ? $param['p'] : '';	
        $url = base64_decode($param['apiurl']);
        if(strpos($url,'?')===false){
            $url .='?';
        }
        else{
            $url .='&';
        }
        $url .= http_build_query($url_param). base64_decode($param['param']);
        $html = zanpian_curl_get($url);	
        if(empty($html)){
			return ['code'=>-1, 'msg'=>'连接API资源库失败，通常为服务器网络不稳定或禁用了采集。'];
		}		
        $json = json_decode($html,true);
		if(is_null($json)){
			return ['code'=>-2, 'msg'=>'JSON格式不正确，不支持采集。'];
		}		
		// 获取到的远程分页数据
		$array_page = array();
		$array_page['pageindex'] = $json['page']['pageindex'];
		$array_page['pagecount'] = $json['page']['pagecount'];
		$array_page['pagesize'] = $json['page']['pagesize'];
		$array_page['recordcount'] = $json['page']['recordcount'];
		if(!in_array($param['ac'], array('ids','day','all','week','post')) ){
		    $param['p'] = 'zanpianpage';
		}
		$array_page['pagelist'] = adminpage($array_page['pageindex'],$array_page['pagecount'],3,url('api',$param),'');
        $model=config('model');		
		// 获取到的远程栏目数据	
		$array_list = array();
		foreach($json['list'] as $key=>$value){
			$array_list[$key]['list_id'] = $value['list_id'];
			$array_list[$key]['list_name'] = $value['list_name'];
			$array_list[$key]['bind_id'] = $value['list_id'];
		}	
		if($json['data']){
			foreach($json['data'] as $key=>$value){
				$json['data'][$key][$model[$param['sid']]['name'].'_cid'] = intval(collect_bind_id($param['apiid'],$value[$model[$param['sid']]['name'].'_cid']));
				$json['data'][$key][$model[$param['sid']]['name'].'_name'] =htmlspecialchars_decode($value[$model[$param['sid']]['name'].'_name']);
			}
		}
		$array['page'] = $array_page; //远程分页数据
		$array['list'] = $array_list; //远程栏目数据
		$array['data'] = $json['data']; //远程内容数据
		return ['code'=>200, 'type'=>'json','data'=>$array];
		
	}	
	public function xml_url_replace($playurl){
		$array_url = array();
		$arr_ji = explode('#',str_replace('||','//',$playurl));
		foreach($arr_ji as $key=>$value){
			$urlji = explode('$',$value);
			if( count($urlji) > 1 ){
				$array_url[$key] = $urlji[0].'$'.trim($urlji[1]);
			}else{
				$array_url[$key] = trim($urlji[0]);
			}
		}
		return implode(chr(13),$array_url);	
	}	
	
	
}